﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace GameAiLaTrieuPhu_Server
{
    public class Server
    {
        private Random random;

        // Root node
        public XmlNode nodeQUESTIONS;

        // Server chose N players
        private int _NPLAYERS;

        public int NPLAYERS
        {
            get { return _NPLAYERS; }
            set { _NPLAYERS = value; }
        }

        // n Players registry
        private int _nPlayers;

        public int nPlayers
        {
            get { return _nPlayers; }
            set { _nPlayers = value; }
        }

        private int _MAX_QUESTIONS;

        public int MAX_QUESTIONS
        {
            get { return _MAX_QUESTIONS; }
            set { _MAX_QUESTIONS = value; }
        }

        private List<Player> listPlayers;        

        private List<Question> listQuestions;

        private int _nQuestions;

        public int nQuestions
        {
            get { return _nQuestions; }
            set { _nQuestions = value; }
        }

        private int _iPlayerNumber;

        public int iPlayerNumber
        {
            get { return _iPlayerNumber; }
            set { _iPlayerNumber = value; }
        }

        private int _iQuestionNumber;

        public int iQuestionNumber
        {
            get { return _iQuestionNumber; }
            set { _iQuestionNumber = value; }
        }

        // Constructor: Construct only nPlayers and listPlayers
        //      + Parameter:    xmlNodeQUESTION: Root node
        //                      iNPLAYERS: Max Player                                       
        public Server(XmlNode xmlNodeQUESTION, int iNPLAYERS)
        {
            nodeQUESTIONS = xmlNodeQUESTION;
            NPLAYERS = iNPLAYERS;
            MAX_QUESTIONS = xmlNodeQUESTION.ChildNodes.Count;
            _nPlayers = 0;
            listPlayers = new List<Player>();
            random = new Random();
        }

        // Method isPossibleNickname is used for check the possibility of this Nickname.
        //      + Parameter: string of Nickname
        //      + Return:   True if this Nickname is possible
        //                  False if this Nickname is impossible
        private bool isPossibleNickname(string strNickname)
        {
            if (0 == nPlayers)
            {
                return true;
            }
            else
            {                
                for (int i = 0; i < _nPlayers; i++)
                {
                    if (strNickname == listPlayers[i].Nickname)
                        return false;
                }
                return true;
            }
        }

        // Method isFullPlayer is used for check the Room is full or not.
        //      + Parameter: none
        //      + Return:   True if the Room is full
        //                  False if the Room is not full
        private bool isFullPlayer()
        {
            if (nPlayers == NPLAYERS)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        // Method AddPlayer is used for add new player
        //      + Parameter: string Nickname
        public void AddPlayer(string strNickname)
        {
            // Check full players
            if (isFullPlayer())
            {
                //Send message to Player
            }
            else
            {
                // Check Nickname is possible
                if (isPossibleNickname(strNickname))
                {
                    nPlayers++;
                    Player player = new Player(nPlayers, strNickname);
                    listPlayers.Add(player);
                }
                else
                {
                    //Send message to Player
                }
            }
        }

        // Mehtod initNQuestion is used for create the random nQuestions
        private int initNQuestions()
        {            
            int iMinRandom = 3 * NPLAYERS;            

            return random.Next(iMinRandom, MAX_QUESTIONS);
        }

        // Method isPossibleNumber is used for check the Number is possible.
        //      + Parameter: list number and number
        //      + Return:   True if the Number is possible
        //                  False if the Number is impossible
        private bool isPossibleNumber(List<int> listNumber, int iNumber)
        {
            if (listNumber.Count > 0)
            {
                for (int i = 0; i < listNumber.Count; i++)
                {
                    if (iNumber == listNumber[i])
                        return false;
                }
                return true;
            }

            return true;
        }

        // Method initListQuestions is used for create the random list questions
        //      + Return: list questions
        private List<Question> initListQuestions()
        {
            List<Question> list = new List<Question>();
            List<int> listNumber = new List<int>();

            for (int i = 0; i < nQuestions; i++)
            {
                int iNumber = random.Next(0, MAX_QUESTIONS);
                if (isPossibleNumber(listNumber, iNumber))
                {
                    listNumber.Add(iNumber);
                }
                else
                {
                    i--;
                }
            }

            for (int i = 0; i < nQuestions; i++)
            {
                Question question = new Question(nodeQUESTIONS.ChildNodes[listNumber[i]]);
                list.Add(question);
            }

            return list;
        }

        public void Start()
        {
            // Init nQuestion and listQuestions
            nQuestions = initNQuestions();            
            listQuestions = initListQuestions();
            random = null;

            // Init iPlayerNumber and iQuestionNumber
            iPlayerNumber = 0;
            iQuestionNumber = 0;
        }

        public string GetQuestionContent()
        {
            return listQuestions[iQuestionNumber].Content;
        }

        public string GetAnswer()
        {
            return listQuestions[iQuestionNumber].listAnswers[0].Content;
        }
    }
}
